package com.example.red_tea_house.moudle.Home.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.red_tea_house.R;
import com.example.red_tea_house.entity.StoryBean;

import com.example.red_tea_house.moudle.Home.adapter.StoryAdapter;
import com.example.red_tea_house.moudle.Write.model.operateStory;
import com.example.red_tea_house.moudle.Write.view.EditActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class WriteFragment extends Fragment {
    private View rootView;
    private FloatingActionButton f_btn;
    private TextView tv_story;
    private TextView tv_mood;
    private TextView tv_draft;
    private SwipeRefreshLayout swipRefreshLayout;
    private RecyclerView recyclerView;
    private RelativeLayout relativeLayout;

    private List<StoryBean> data=new ArrayList<>();

    private int returnStory=1;

    public WriteFragment() {
    }

    public static WriteFragment newInstance() {
        WriteFragment fragment = new WriteFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.write_page_layout, container, false);
        bindItems();

        tv_story.setOnClickListener((v)->{
            relativeLayout.setBackgroundResource(R.color.teal_200);
            readStoryFromDB();
            refreshItems();
       });

        tv_mood.setOnClickListener((v)->{
            relativeLayout.setBackgroundResource(R.color.colorWhite);
            refreshItems();
        });

        tv_draft.setOnClickListener((v)->{
            relativeLayout.setBackgroundResource(R.color.purple_200);
            readDraftFromDB();
            refreshItems();
        });

        f_btn.setOnClickListener((v)-> {
            Intent intent=new Intent(getActivity(), EditActivity.class);
            startActivity(intent);
        });
        swipRefreshLayout.setOnRefreshListener(()->{
            refreshItems();
        });
        setDefaultPage();
        refreshItems();
        return rootView;
    }
    private void bindItems(){
        f_btn = rootView.findViewById(R.id.floatingActionButton);
        tv_draft=rootView.findViewById(R.id.my_draft_0);
        tv_mood=rootView.findViewById(R.id.my_mood_0);
        tv_story=rootView.findViewById(R.id.story_0);
        swipRefreshLayout=rootView.findViewById(R.id.WriteLayoutRefreshLayout);
        relativeLayout =rootView.findViewById(R.id.write_layout_relativelayout);
        recyclerView=rootView.findViewById(R.id.write_main_recycler);
    }
    private void readStoryFromDB(){
        data=operateStory.loadLocalStory();
    }
    private void readDraftFromDB() {data=operateStory.loadDraft();}
    private void setDefaultPage(){tv_story.performClick();}

    private void refreshItems(){
        setLayout();
        if(swipRefreshLayout.isRefreshing()){
            swipRefreshLayout.setRefreshing(false);
        }
    }
    private void setLayout(){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        StoryAdapter storyAdapter=new StoryAdapter(data,getActivity());
        recyclerView.setAdapter(storyAdapter);
    }
}